import HTTP_CODE from '../constant/http-code'
import * as request from '../constant/fetch-request'
import message from 'antd/es/message/index'
import {convertContent} from '../constant/lang-util'

export const refreshMyStudents = myStudents => ({
  type: 'REFRESH_MY_STUDENTS',
  myStudents
})
export const refreshMyStudentsAssignmentStatus = myStudentsAssignmentInfo => ({
  type: 'REFRESH_STUDENTS_ASSIGNMENTS',
  myStudentsAssignmentInfo
})

export const refreshAssignmentStudents = assignmentStudents => ({
  type: 'REFRESH_ASSIGNMENT_STUDENTS',
  assignmentStudents
})

export const getMyStudents = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/myStudents/programs/${programId}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshMyStudents(res.body))
      }
    })()
  }
}
export const getMyStudentsSectionsStatus = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/myStudents/programs/${programId}/students/assignments`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshMyStudentsAssignmentStatus(res.body))
      }
    })()
  }
}
export const getStudentAssignmentStatus = (programId, assignmentId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/myStudents/programs/${programId}/assignments/${assignmentId}/students`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshAssignmentStudents({assignmentId, ...res.body}))
      }
    })()
  }
}

export const follow = (followerId, programId, callback) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/myStudents/programs/${programId}/followers/${followerId}`)
      if (res.status === HTTP_CODE.CREATED) {
        message.success(convertContent('关注成功'))
        callback()
        dispatch(getMyStudents(programId))
      }
    })()
  }
}

export const unFollow = (followerId, programId) => {
  return dispatch => {
    (async () => {
      const res = await request.del(`../api/myStudents/programs/${programId}/followers/${followerId}`)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        message.success(convertContent('取消关注成功'))
        dispatch(getMyStudents(programId))
      }
    })()
  }
}

export const addConcealComment = (studentId, programId, content) => {
  const data = {
    studentId,
    programId,
    content
  }
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/myStudents/concealComment`, data)
      if (res.status === HTTP_CODE.CREATED) {
        message.success(convertContent('提交成功'))
        dispatch(getMyStudents(programId))
      }
    })()
  }
}

export const batchFollow = (programId, tutorId, studentIds, callback) => {
  return (dispatch) => {
    (async () => {
      const res = await request.post(`../api/programs/${programId}/tutors/${tutorId}/students/${studentIds}`)
      if (res.status === HTTP_CODE.CREATED) {
        message.success(convertContent('添加成功'))
        dispatch(getMyStudents(programId))
        callback(studentIds, programId)
      }
    })()
  }
}
export const batchUnFollow = (programId, tutorId, studentIds) => {
  return (dispatch) => {
    (async () => {
      const res = await request.del(`../api/programs/${programId}/tutors/${tutorId}/students/${studentIds}`)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        message.success(convertContent('取消关注'))
        dispatch(getMyStudents(programId))
      }
    })()
  }
}
